<template>
  <div class="quiz-container">
    <div class="filter-bar">
      <div class="filter-item">
        <input type="text" placeholder="题型筛选" class="compact-input" />
        <i class="iconfont icon-filter"></i>
      </div>
      <div class="filter-item">
        <input type="text" placeholder="题库搜索" class="compact-input" />
        <i class="iconfont icon-search"></i>
      </div>
    <slot name="action" :selectedQuestions="selectedQuestions" >
           <button @click="confirmQuestion()"   class="primary-btn">
        <i class="iconfont icon-upload"></i> 导入{{ selectedQuestions.length }}题
      </button>
     </slot>


    </div>

    <div class="questions-panel">
      <div v-for="(question, index) in questions" class="question-card"   :class="{ 'selected': question.selected }"  @click="toggleSelection(question)">
        <div class="card-header">
          <input type="checkbox" v-model="question.selected" class="item-checkbox" />
          <div class="type-tag">{{ question.quTypeDictText }}</div>
        </div>
        <Single v-if="question.quType == 1" :question="question"></Single>
        <Multipe  v-if="question.quType == 2" :question="question"></Multipe>
        <ShortAnswer  v-if="question.quType == 4" :question="question"></ShortAnswer>
        <Judgment v-if="question.quType == 3" :question="question"></Judgment>
        <FillBank  v-if="question.quType == 5" :question="question"></FillBank>
      </div>

    </div>

    <Pagination
      :totalItems="totalPages"
      :itemsPerPage="4"
      :initialPage="1"
      @page-change="handlePageChange"
    />
    <Loading
      :size="60"
      color="#4CAF50"
      shadowColor="#A5D6A7"
      :speed="0.6"
      :loading="loading"
      backgroundColor="rgba(255, 255, 255, 0.8)">
</Loading>
  </div>


</template>

<script setup>
import { ref, onMounted, onBeforeMount ,defineProps} from 'vue';
import { QuestionService } from '@/api/questionApi';
import Single from '../questionnModel/Single.vue';
import Multipe from '@/components/exam/questionnModel/Multipe.vue'
import ShortAnswer from '../questionnModel/ShortAnswer.vue';
import Judgment from '../questionnModel/Judgment.vue';
import FillBank from '../questionnModel/FillBank.vue';
import Pagination from '@/components/commont/Pagination.vue';
import Loading from '@/components/commont/Loading.vue';
import { QuestionBankService }  from '@/api/questionBank.ts';

const loading=ref(false);
const { queryQuestionBank} = QuestionBankService;
const questions = ref([]);
const selectedQuestions = ref([]);
const emit = defineEmits(['confirmQuestion'])
const props=defineProps(['quType','questionBankId','isManage'])
const bankId=props.questionBankId;
const currentQuType=props.quType;
const totalPages=ref(1);
const  currentPage=ref(1);





const confirmQuestion = () => {
emit("confirmQuestion",selectedQuestions.value)
};


const getDifficultyColor = (level) => {
  const colors = {
    '简单': '#4CAF50',
    '中等': '#FFA726',
    '困难': '#EF5350'
  };
  return colors[level] || '#666';
};

const toggleSelection = (question) => {
  question.selected = !question.selected; // 切换选中状态

  if (question.selected) {
    // 如果选中，添加到数组中（避免重复）
    if (!selectedQuestions.value.includes(question)) {
      selectedQuestions.value.push(question);
    }
  } else {
    // 如果取消选中，从数组中移除
    const index = selectedQuestions.value.indexOf(question);
    if (index > -1) {
      selectedQuestions.value.splice(index, 1);
    }
  }
  console.log(selectedQuestions.value)
};

const  handlePageChange = (pageData)=>{
  currentPage.value = pageData.page;

  loadQuestions(currentPage.value);
 console.log(pageData)
};

const loadQuestions = async (page) => {
  loading.value = true;
  try {
    let response;
    if (currentQuType) {
      response = await QuestionService.getQuestionType({
        page,
        pageSize: 6,
        quType: currentQuType
      });
    } else {
      response = await queryQuestionBank({
        pageNum: page,
        pageSize: 10,
        bankId: props.isManage ? bankId : null
      });
    }
    const { data = [], map = {} } = response || {};
    totalPages.value = map.totalPage || 1;
    currentPage.value = map.currentPage || page;
    questions.value = data.map(obj => ({
      ...obj,
      showExplanation: false,
      selected: false
    }));
    console.log("子组件数据加载完成:", data);
  } catch (error) {
    console.error("加载题目失败:", error);
  } finally {
    loading.value = false;
  }
};



onBeforeMount(() => {

});
onMounted(() => {
  loadQuestions(currentPage.value)
});
const toggleExplanation = (question) => {
  question.showExplanation = !question.showExplanation;
};




</script>

<style scoped >
.quiz-container {
  max-width:100% ;
  padding: 1.5rem;
  margin: 0 auto;
  font-family: ui-sans-serif, system-ui, sans-serif;
  font-size: 1.18 cap;
  overflow-x: hidden;
 padding-bottom: 0px;
}

.filter-bar {
  display: flex;
  gap: 1rem;
  margin-bottom: 1.5rem;
}

.compact-input {
  padding: 0.8rem 2.5rem 0.8rem 1rem;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  position: relative;
  flex: 1;
  transition: all 0.2s;
}

.compact-input:focus {
  border-color: #9cdb9e;
  box-shadow: 0 0 0 2px rgba(156, 219, 158, 0.1);
}

.compact-input::placeholder {
  color: #999;
  font-size: 0.8rem;
}

.filter-item {
  position: relative;
  flex: 1;
}

.filter-item .iconfont {
  position: absolute;
  right: 0.8rem;
  top: 50%;
  transform: translateY(-50%);
  color: #5bc50f;
  font-size: 1rem;
}

.primary-btn {
  padding: 0.8rem 1.5rem;
  background: linear-gradient(135deg, #9cdb9e 0%, #8cc68c 100%);
  color: white;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.2s;
}

.primary-btn:hover {
  background: linear-gradient(135deg, #a8e2a9 0%, #9cdb9e 100%);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.questions-panel {
  display: flex;
  flex-direction: column;
  gap: 1.2rem;
}

.question-card {
  background: white;
  border-radius: 8px;
  border: 0.5px solid rgba(0, 0, 0, 0.155);
  padding: 0.8rem;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.05);
  transition: all 0.1s;
  cursor: pointer;

}

.question-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.question-card.selected {
  border: 2px solid #4790f6d2;
  background: rgba(0, 100, 232, 0.05);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.4rem;
}

.type-tag {
  background: #9cdb9e;
  color: white;
  padding: 0.05rem 0.6rem;
  border-radius: 4px;
  font-size: 0.8rem;
}

.source-info {
  color: #666;
  font-size: 0.9rem;
}

.content-section {
  margin-bottom: 1rem;
}

.question-content {
  background: #f9f9f9;
  padding: 1rem;
  border-radius: 8px;
  margin-bottom: 1rem;
  position: relative;
}

.question-content::before {
  content: '';
  position: absolute;
  left: -10px;
  top: 10px;
  width: 3px;
  height: 30px;
  background: #9cdb9e;
}

.content-tag {
  background: #9cdb9e;
  color: white;
  padding: 0.2rem 0.5rem;
  border-radius: 3px;
  margin-right: 0.5rem;
}

.options-section {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.icon-check {
  color: #9cdb9e;
  margin-left: auto;
}

.card-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-top: 1px solid #eee;
  padding-top: 1rem;
}

.difficulty-level {
  padding: 0.3rem 0.8rem;
  border-radius: 12px;
  color: white;
  font-size: 0.9rem;
}

.action-btns {
  color: rgba(0, 0, 0, 0.639);
  cursor: pointer;
  font-size: medium;
  font-weight: 600;
  text-decoration: 3px underline #3bd16a;
}

.action-btns:hover {
  text-decoration: 3px underline #fc1307;
}

.explanation-panel {
  padding: 1rem;
  background: #f9f9f9;
  border-radius: 8px;
  margin-top: 1rem;
}

.slide-enter-active,
.slide-leave-active {
  transition: max-height 0.3s ease-out;
}

.slide-enter-from,
.slide-leave-to {
  max-height: 0;
  overflow: hidden;
}

.iconfont {
  font-family: 'iconfont' !important;
  font-size: 1rem;
}

</style>
